<?php

namespace yiike\store\backend\controllers;

use dmstr\bootstrap\Tabs;
use yii\web\HttpException;
use yii\helpers\Url;
use yii\filters\AccessControl;
use common\models\Region;
use yiike\store\model\TtycCategory;

/**
 * This is the class for controller "TtycStoreController".
 */
class TtycStoreController extends \yiike\store\backend\controllers\base\TtycStoreController
{
    public function actionUpdate($id)
    {
        $cateModel = new TtycCategory();
        $category = $cateModel->find()->select('id,name,parentid')->where(['parentid' => 0, 'enabled' => 1])->asArray()->all();
        $cate = $cateModel->find()->select('id,name,parentid')->where(['<>', 'parentid', 0])->andWhere(['enabled' => 1])->asArray()->all();

        foreach ($category as $k => $v) {
            $arr[$v['id']] = $v['name'];
        }
        foreach ($arr as $k => $v) {
            foreach ($cate as $m => $n) {
                if ($n['parentid'] == $k) {
                    $arr[$v][$n['id']] = $n['name'];
                }
            }
        }
        $category = $arr;
        $model = $this->findModel($id);
        $model->province = Region::getCityId($model->location_p);
        $model->city = Region::getCity($model->location_c, $model->province);
        $model->district = Region::getCity($model->location_a, $model->city);
        if ($model->load($_POST)) {

            $province = $_POST['Store']['province'];
            $province = Region::getCityName($province);
            $city = $_POST['Store']['city'];
            $city = Region::getCityName($city);
            $district = $_POST['Store']['district'];
            $district = Region::getCityName($district);
            $cate = $_POST['Store']['ccate'];
            $parent = \Yii::$app->db->createCommand("SELECT parentid FROM ims_ttyc_category WHERE weid = 2 and id=$cate ")->queryOne();
            $parent = $parent['parentid'];
            if ($parent == 0) {
                $model->pcate = $cate;
                $model->ccate = $cate;
            } else {
                $model->pcate = $parent;
                $model->ccate = $cate;
            }
            $model->discounts = '0';
            $model->location_p = $province;
            $model->location_c = $city;
            $model->location_a = $district;
            if ($model->save()) {
                return $this->redirect(['ttyc-store/index']);
            } else {
                return $this->render('update', [
                    'model' => $model,
                    'category' => $category,
                ]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
                'category' => $category,
            ]);
        }
    }

    //省市县三级
    public function actions()
    {
        $actions = parent::actions();
        $actions['get-region'] = [
            'class' => \chenkby\region\RegionAction::className(),
            'model' => \common\models\Region::className()
        ];
        return $actions;
    }
}
